package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.AttendanceDao;
import com.pentofab.payrollmanagement.dao.EmployeeDao;
import com.pentofab.payrollmanagement.dao.model.Attendance;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.Department;
import com.pentofab.payrollmanagement.dao.model.Units;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.ui.control.CustomTime;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class AddAttendanceEntryController extends AbstractUIController
implements FormValidator {

	
	HashMap<Integer,ArrayList<String>> timeCountMap = new HashMap<Integer, ArrayList<String>>();
	@FXML
	private GridPane dataGrid; 
	@FXML
	private ChoiceBox<Department> dept; 
	@FXML
	private ChoiceBox<String> from_date; 
	@FXML
	private ChoiceBox<String> from_month; 
	@FXML
	private ChoiceBox<String> from_year; 
	@FXML
	public GridPane root; 
	@FXML
	private ChoiceBox<Units> unit;
	
	@FXML CustomTime row1col3;
	@FXML CustomTime row1col2;
    @FXML CustomTime row1col4;
    @FXML CustomTime row1col5;
    @FXML CustomTime row1col6;
    @FXML CustomTime row1col7;
    @FXML CustomTime row2col2;
    @FXML CustomTime row2col3;
    @FXML CustomTime row2col4;
    @FXML CustomTime row2col5;
    @FXML CustomTime row2col6;
    @FXML CustomTime row2col7;
    @FXML CustomTime row3col2;
    @FXML CustomTime row3col3;
    @FXML CustomTime row3col4;
    @FXML CustomTime row3col5;
    @FXML CustomTime row3col6;
    @FXML CustomTime row3col7;
    @FXML CustomTime row4col2;
    @FXML CustomTime row4col3;
    @FXML CustomTime row4col4;
    @FXML CustomTime row4col5;
    @FXML CustomTime row4col6;
    @FXML CustomTime row4col7;
    @FXML CustomTime row5col2;
    @FXML CustomTime row5col3;
    @FXML CustomTime row5col4;
    @FXML CustomTime row5col5;
    @FXML CustomTime row5col6;
    @FXML CustomTime row5col7;
    @FXML CustomTime row6col2;
    @FXML CustomTime row6col3;
    @FXML CustomTime row6col4;
    @FXML CustomTime row6col5;
    @FXML CustomTime row6col6;
    @FXML CustomTime row6col7;
    @FXML CustomTime row7col2;
    @FXML CustomTime row7col3;
    @FXML CustomTime row7col4;
    @FXML CustomTime row7col5;
    @FXML CustomTime row7col6;
    @FXML CustomTime row7col7;
    @FXML CustomTime row8col2;
    @FXML CustomTime row8col3;
    @FXML CustomTime row8col4;
    @FXML CustomTime row8col5;
    @FXML CustomTime row8col6;
    @FXML CustomTime row8col7;
    @FXML CustomTime row9col2;
    @FXML CustomTime row9col3;
    @FXML CustomTime row9col4;
    @FXML CustomTime row9col5;
    @FXML CustomTime row9col6;
    @FXML CustomTime row9col7;
    @FXML CustomTime row10col2;
    @FXML CustomTime row10col3;
    @FXML CustomTime row10col4;
    @FXML CustomTime row10col5;
    @FXML CustomTime row10col6;
    @FXML CustomTime row10col7;
    @FXML CustomTime row11col2;
    @FXML CustomTime row11col3;
    @FXML CustomTime row11col4;
    @FXML CustomTime row11col5;
    @FXML CustomTime row11col6;
    @FXML CustomTime row11col7;
    @FXML CustomTime row12col2;
    @FXML CustomTime row12col3;
    @FXML CustomTime row12col4;
    @FXML CustomTime row12col5;
    @FXML CustomTime row12col6;
    @FXML CustomTime row12col7;
    @FXML CustomTime row13col2;
    @FXML CustomTime row13col3;
    @FXML CustomTime row13col4;
    @FXML CustomTime row13col5;
    @FXML CustomTime row13col6;
    @FXML CustomTime row13col7;
    @FXML CustomTime row14col2;
    @FXML CustomTime row14col3;
    @FXML CustomTime row14col4;
    @FXML CustomTime row14col5;
    @FXML CustomTime row14col6;
    @FXML CustomTime row14col7;
    @FXML CustomTime row15col2;
    @FXML CustomTime row15col3;
    @FXML CustomTime row15col4;
    @FXML CustomTime row15col5;
    @FXML CustomTime row15col6;
    @FXML CustomTime row15col7;
    @FXML TextField row1col8;
    @FXML TextField row2col8;
    @FXML TextField row3col8;
    @FXML TextField row4col8;
    @FXML TextField row5col8;
    @FXML TextField row6col8;
    @FXML TextField row7col8;
    @FXML TextField row8col8;
    @FXML TextField row9col8;
    @FXML TextField row10col8;
    @FXML TextField row11col8;
    @FXML TextField row12col8;
    @FXML TextField row13col8;
    @FXML TextField row14col8;
    @FXML TextField row15col8;
	@FXML TextField row1col0;
    @FXML TextField row2col0;
    @FXML TextField row3col0;
    @FXML TextField row4col0;
    @FXML TextField row5col0;
    @FXML TextField row6col0;
    @FXML TextField row7col0;
    @FXML TextField row8col0;
    @FXML TextField row9col0;
    @FXML TextField row10col0;
    @FXML TextField row11col0;
    @FXML TextField row12col0;
    @FXML TextField row13col0;
    @FXML TextField row14col0;
    @FXML TextField row15col0;
    @FXML TextField row1col1;
    @FXML TextField row2col1;
    @FXML TextField row3col1;
    @FXML TextField row4col1;
    @FXML TextField row5col1;
    @FXML TextField row6col1;
    @FXML TextField row7col1;
    @FXML TextField row8col1;
    @FXML TextField row9col1;
    @FXML TextField row10col1;
    @FXML TextField row11col1;
    @FXML TextField row12col1;
    @FXML TextField row13col1;
    @FXML TextField row14col1;
    @FXML TextField row15col1;
    
    LinkedList<CustomTime> inOne = new LinkedList<CustomTime>();
    LinkedList<CustomTime> outOne = new LinkedList<CustomTime>();
    LinkedList<CustomTime> inTwo = new LinkedList<CustomTime>();
    LinkedList<CustomTime> outTwo = new LinkedList<CustomTime>();
    LinkedList<CustomTime> inThr = new LinkedList<CustomTime>();
    LinkedList<CustomTime> outThr = new LinkedList<CustomTime>();
    LinkedList<TextField> totalFields = new LinkedList<TextField>();
    LinkedList<TextField> empId = new LinkedList<TextField>();
    LinkedList<TextField> empName = new LinkedList<TextField>();
    int maxPoputated= 0;
    
	private Company company;
	private UserAccounts accounts; 
	EmployeeDao employeeDao = PayrollApplicationFactory.getEmployeeMgmt();
	AttendanceDao attendanceDao = PayrollApplicationFactory.getAttandanceMgmt();
	private ArrayList<String> fromMonthDaysList = new ArrayList<String>();
	private int startedIndex = -1;
	private int endIngEndex = -1;
	LinkedHashMap<String, ArrayList<Attendance>>  attInfoList;
	LinkedList<String> employeeIDList = new LinkedList<String>();
	
	public void next() {
		
		System.out.println("startedIndex  "+startedIndex);
		System.out.println("endIngEndex  "+endIngEndex);
		System.out.println("maxPoputated : "+maxPoputated);
		System.out.println("employeeIDList size  : "+employeeIDList.size());
		int gridSize = 15;
		
		
		
		if(startedIndex < 0){
			return;
		}
//		maxPoputated = 0;
		int curCount = 0;
		
		
		if(maxPoputated >= endIngEndex){
			return;
		}
		cleartextFields();
		for(int i = startedIndex ;(i< endIngEndex && curCount < gridSize ); i++){
			
			String employeeId = employeeIDList.get(i);
			 ArrayList<Attendance> attList = attInfoList.get(employeeId);
			 int rem = 0;
			 
			 while(attList.size() > 3){
				 
				 attList.remove(rem);
			 }
			 Attendance one = attList.get(0);
			 Attendance two = new Attendance();
			 if(attList.size() > 1){
				 two = attList.get(1);
			 }
			 Attendance three =  new Attendance();
			 
			 if(attList.size() > 2){
				 three = attList.get(2);
			 }
			 
			 empId.get(curCount).setText(one.getEmployee().getEmpId());
			 empName.get(curCount).setText(one.getEmployee().getEmpName());
			 
			 if(one.getLoginTime() != null){
				 
				 inOne.get(curCount).setText(one.getLoginTime());
				 enable( inOne.get(curCount));
			 }
			 if(one.getLogoutTime() != null){
				 outOne.get(curCount).setText(one.getLogoutTime() );
				 enable( outOne.get(curCount));
			 }

			 if(two.getLoginTime() != null){
				 
				 inTwo.get(curCount).setText(two.getLoginTime() );
				 enable( inTwo.get(curCount));
			 }
			 if(two.getLogoutTime() != null){
				 
				 outTwo.get(curCount).setText(two.getLogoutTime() );
				 enable( outTwo.get(curCount));
			 }


			 if(three.getLoginTime() != null){
				 
				 inThr.get(curCount).setText(three.getLoginTime() );
				 enable( inThr.get(curCount));
			 }
			 if(three.getLogoutTime() != null){
				 
				 outThr.get(curCount).setText(three.getLogoutTime() );
				 enable( outThr.get(curCount));
			 }

			 maxPoputated++;
			 curCount++;
		}
		
		startedIndex = maxPoputated;
		
	}

	public void reset() {
		
		refreshUI();
	}

	public void save() {
		
		System.out.println("save ========== save ==========save ");
		int gridSize = 16;
		
		try {
			
			for(int i = 0 ;i < maxPoputated; i++){
				
				String curEmpsId = empId.get(i).getText();
				String total = totalFields.get(i).getText();

				if(!PayRollUtility.validateTime(total)){
					showError("Please correct In adnd out time for employee"+curEmpsId+" as total time ["+total+" ] exeeds 24 hrs");
				}

				String fromDate = from_date.getSelectionModel().getSelectedItem()
						+ "-"+ (from_month.getSelectionModel().getSelectedIndex() + 1)
						+ "-"+ from_year.getSelectionModel().getSelectedItem();


				if(inOne.get(i).getText() != null && !inOne.get(i).getText().equals("")){

					attendanceDao.addAttandeanceEntry(curEmpsId,PayRollUtility.convertStringToDate(fromDate), inOne.get(i).getText(), 
							outOne.get(i).getText(), accounts.getUsername(), 1, true, false);
				}

				if(inTwo.get(i).getText() != null && !inTwo.get(i).getText().equals("")){

					attendanceDao.addAttandeanceEntry(curEmpsId,PayRollUtility.convertStringToDate(fromDate), inTwo.get(i).getText(), 
							outTwo.get(i).getText(), accounts.getUsername(), 2, false, false);
				}

				if(inThr.get(i).getText() != null && !inThr.get(i).getText().equals("")){

					attendanceDao.addAttandeanceEntry(curEmpsId,PayRollUtility.convertStringToDate(fromDate), inThr.get(i).getText(), 
							outThr.get(i).getText(), accounts.getUsername(), 3, false, true);
				}


			}
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
		}
		System.out.println("save ========== save ==========save end");
	}

	@Override 
	public void initialize(URL fxmlFileLocation, ResourceBundle resources) {

		
		from_year.getSelectionModel().selectedIndexProperty()
		.addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue arg0, Number arg1,
					Number arg2) {
				from_month.getSelectionModel().clearSelection();
			}

		});
		from_month.getSelectionModel().selectedIndexProperty()
				.addListener(new ChangeListener<Number>() {

					@Override
					public void changed(ObservableValue arg0, Number arg1,
							Number arg2) {

						from_date.getSelectionModel().clearSelection();
						from_date.getItems().clear();
						fromMonthDaysList.clear();
						
						if(from_month.getSelectionModel().getSelectedIndex()< 0)
							return;
						System.out.println(from_date.getItems());
						int fromMaxDay = PayRollUtility.getMaxDayofMonth(Integer
								.parseInt(from_year.getSelectionModel()
										.getSelectedItem()), from_month
								.getSelectionModel().getSelectedIndex());
						for (int i = 1; i <= fromMaxDay; i++) {

							fromMonthDaysList.add(i + "");
						}
						from_date.getItems().addAll(fromMonthDaysList);

					}

				});
		
		inOneField();
		inTwoField();
		inThreeField();
		outOneField();
		outTwoField();
		outThreeField();
		totalFields();
		empIdFieds();
		empNameFieds();
		
		Iterator<CustomTime> listIterator = inOne.iterator();
		
		while(listIterator.hasNext()){
			inOneFieldFocusHandler(listIterator.next());
		}
		
		listIterator = outOne.iterator();
		
		while(listIterator.hasNext()){
			outOneFieldFocusHandler(listIterator.next());
		}
		
		listIterator = inTwo.iterator();
		
		while(listIterator.hasNext()){
			inTwoFieldFocusHandler(listIterator.next());
		}
		
		listIterator = outTwo.iterator();
		
		while(listIterator.hasNext()){
			outTwoFieldFocusHandler(listIterator.next());
		}
		
		listIterator = inThr.iterator();
		
		while(listIterator.hasNext()){
			inThrFieldFocusHandler(listIterator.next());
		}
		
		listIterator = outThr.iterator();
		
		while(listIterator.hasNext()){
			outThrFieldFocusHandler(listIterator.next());
		}
	}

	@Override
	public boolean isValid() {
		return false;
	}

	@Override
	public String getValidationMessage() {
		return null;
	}

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "Add Attendance Entry";
	}

	@Override
	public void refreshUI(Object... args) {
		maxPoputated = 0;
		
		from_date.setDisable(false);
		from_month.setDisable(false);
		from_year.setDisable(false);
		cleartextFields();
		startedIndex = -1;
		endIngEndex = -1;
		employeeIDList = new LinkedList<String>();
		if (args != null && args.length > 0) {
			Company company = (Company) args[0];
			this.company = (Company) args[0];;
			
			if(args.length > 1 && args[1] instanceof UserAccounts){
				this.accounts = (UserAccounts) args[1];
			}
		}
		
		int year = PayRollUtility.getYear() - 1;
		ArrayList<String> yearList = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			yearList.add(year + "");
			year++;
		}
		from_year.getItems().addAll(yearList);
		try{
			List<Units> unitList = employeeDao.getUnits(company.getCompanyID());
			ObservableList<Units> unitStringList = FXCollections.observableArrayList();
			unitStringList.addAll(unitList);
			this.unit.getItems().clear();
			this.unit.getItems().addAll(unitStringList);
			
			List<Department> departments = employeeDao.getAllDepartments(this.company.getCompanyID());
			ObservableList<Department> departmentsObservableList = FXCollections.observableArrayList();
			departmentsObservableList.addAll(departments);
			this.dept.getItems().clear();
			this.dept.getItems().addAll(departmentsObservableList);
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		disableFields(inOne);
		disableFields(outOne);
		disableFields(inTwo);
		disableFields(outTwo);
		disableFields(inThr);
		disableFields(outThr);
		
	}
	
	public void retrive() {
		try{
			cleartextFields();
			maxPoputated = 0;
			String fromDate = from_date.getSelectionModel()
					.getSelectedItem()
					+ "-"
					+ (from_month.getSelectionModel()
							.getSelectedIndex() + 1)
					+ "-"
					+ from_year.getSelectionModel()
							.getSelectedItem();
			fromDate = PayRollUtility.convertDateToString(PayRollUtility.convertStringToDate(fromDate));
			from_date.setDisable(true);
			from_month.setDisable(true);
			from_year.setDisable(true);
			
			Units selunit =  unit.getSelectionModel().getSelectedItem();
			Department selDept = dept.getSelectionModel().getSelectedItem();
			String unitCode =  (selunit != null) ? selunit.getUnitId() : null;
			String deptCode =  (selDept != null) ? selDept.getCode() : null;
			attInfoList = attendanceDao.getAttEntry(fromDate, company.getCompanyID(),unitCode,deptCode);
			System.out.println("attInfoList : "+attInfoList);
			employeeIDList.clear();
			employeeIDList.addAll(attInfoList.keySet());
			System.out.println("emIdList : "+ employeeIDList);
			if(attInfoList.size() > 0){
				
				System.out.println("attInfoList : "+attInfoList.size());
				startedIndex = 0;
				endIngEndex = attInfoList.size();
				next();
			}
		}catch(Exception e){
			e.printStackTrace();
			showError(e.getMessage());
			refreshUI();
		}
		
	}
	
	private void disableFields(LinkedList<CustomTime> outOne2) {
		
		Iterator<CustomTime> listIterator = outOne2.iterator();
		
		while(listIterator.hasNext()){
			
			CustomTime field = listIterator.next();
			field.setEditable(false);
			field.setFocusTraversable(false);
		}
		
	}

	private void cleartextFields(){
		
		row1col3.clear();
		row1col2.clear();
		row1col4.clear();
		row1col5.clear();
		row1col6.clear();
		row1col7.clear();
		
		row2col2.clear();
		row2col3.clear();
		row2col4.clear();
		row2col5.clear();
		row2col6.clear();
		row2col7.clear();
		row3col2.clear();
		row3col3.clear();
		row3col4.clear();
		row3col5.clear();
		row3col6.clear();
		row3col7.clear();
		row4col2.clear();
		row4col3.clear();
		row4col4.clear();
		row4col5.clear();
		row4col6.clear();
		row4col7.clear();
		row5col2.clear();
		row5col3.clear();
		row5col4.clear();
		row5col5.clear();
		row5col6.clear();
		row5col7.clear();
		row6col2.clear();
		row6col3.clear();
		row6col4.clear();
		row6col5.clear();
		row6col6.clear();
		row6col7.clear();
		row7col2.clear();
		row7col3.clear();
		row7col4.clear();
		row7col5.clear();
		row7col6.clear();
		row7col7.clear();
		row8col2.clear();
		row8col3.clear();
		row8col4.clear();
		row8col5.clear();
		row8col6.clear();
		row8col7.clear();
		row9col2.clear();
		row9col3.clear();
		row9col4.clear();
		row9col5.clear();
		row9col6.clear();
		row9col7.clear();
		row10col2.clear();
		row10col3.clear();
		row10col4.clear();
		row10col5.clear();
		row10col6.clear();
		row10col7.clear();
		row11col2.clear();
		row11col3.clear();
		row11col4.clear();
		row11col5.clear();
		row11col6.clear();
		row11col7.clear();
		row12col2.clear();
		row12col3.clear();
		row12col4.clear();
		row12col5.clear();
		row12col6.clear();
		row12col7.clear();
		row13col2.clear();
		row13col3.clear();
		row13col4.clear();
		row13col5.clear();
		row13col6.clear();
		row13col7.clear();
		row14col2.clear();
		row14col3.clear();
		row14col4.clear();
		row14col5.clear();
		row14col6.clear();
		row14col7.clear();
		row15col2.clear();
		row15col3.clear();
		row15col4.clear();
		row15col5.clear();
		row15col6.clear();
		row15col7.clear();
		
		row1col8.clear();
		row2col8.clear();
		row3col8.clear();
		row4col8.clear();
		row5col8.clear();
		row6col8.clear();
		row7col8.clear();
		row8col8.clear();
		row9col8.clear();
		row10col8.clear();
		row11col8.clear();
		row12col8.clear();
		row13col8.clear();
		row14col8.clear();
		row15col8.clear();
		
		 row1col0.clear();
		 row2col0.clear();
		 row3col0.clear();
		 row4col0.clear();
		 row5col0.clear();
		 row6col0.clear();
		 row7col0.clear();
		 row8col0.clear();
		 row9col0.clear();
		 row10col0.clear();
		 row11col0.clear();
		 row12col0.clear();
		 row13col0.clear();
		 row14col0.clear();
		 row15col0.clear();
		 row1col1.clear();
		 row2col1.clear();
		 row3col1.clear();
		 row4col1.clear();
		 row5col1.clear();
		 row6col1.clear();
		 row7col1.clear();
		 row8col1.clear();
		 row9col1.clear();
		 row10col1.clear();
		 row11col1.clear();
		 row12col1.clear();
		 row13col1.clear();
		 row14col1.clear();
		 row15col1.clear();

		System.out.println(dataGrid.getColumnIndex(row3col8));
		
	}
	
	public void goBack() {

		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	
	}
	
	private void inOneField(){
		
		inOne.add(row1col2);
		inOne.add(row2col2);
		inOne.add(row3col2);
		inOne.add(row4col2);
		inOne.add(row5col2);
		inOne.add(row6col2);
		inOne.add(row7col2);
		inOne.add(row8col2);
		inOne.add(row9col2);
		inOne.add(row10col2);
		inOne.add(row11col2);
		inOne.add(row12col2);
		inOne.add(row13col2);
		inOne.add(row14col2);
		inOne.add(row15col2);
	}
	
	private void outOneField(){
		
		outOne.add(row1col3);
		outOne.add(row2col3);
		outOne.add(row3col3);
		outOne.add(row4col3);
		outOne.add(row5col3);
		outOne.add(row6col3);
		outOne.add(row7col3);
		outOne.add(row8col3);
		outOne.add(row9col3);
		outOne.add(row10col3);
		outOne.add(row11col3);
		outOne.add(row12col3);
		outOne.add(row13col3);
		outOne.add(row14col3);
		outOne.add(row15col3);
	}
	
	private void inTwoField(){
		
		inTwo.add(row1col4);
		inTwo.add(row2col4);
		inTwo.add(row3col4);
		inTwo.add(row4col4);
		inTwo.add(row5col4);
		inTwo.add(row6col4);
		inTwo.add(row7col4);
		inTwo.add(row8col4);
		inTwo.add(row9col4);
		inTwo.add(row10col4);
		inTwo.add(row11col4);
		inTwo.add(row12col4);
		inTwo.add(row13col4);
		inTwo.add(row14col4);
		inTwo.add(row15col4);
	}
	
	private void outTwoField(){
		
		outTwo.add(row1col5);
		outTwo.add(row2col5);
		outTwo.add(row3col5);
		outTwo.add(row4col5);
		outTwo.add(row5col5);
		outTwo.add(row6col5);
		outTwo.add(row7col5);
		outTwo.add(row8col5);
		outTwo.add(row9col5);
		outTwo.add(row10col5);
		outTwo.add(row11col5);
		outTwo.add(row12col5);
		outTwo.add(row13col5);
		outTwo.add(row14col5);
		outTwo.add(row15col5);
	}
	
	private void inThreeField(){
		
		inThr.add(row1col6);
		inThr.add(row2col6);
		inThr.add(row3col6);
		inThr.add(row4col6);
		inThr.add(row5col6);
		inThr.add(row6col6);
		inThr.add(row7col6);
		inThr.add(row8col6);
		inThr.add(row9col6);
		inThr.add(row10col6);
		inThr.add(row11col6);
		inThr.add(row12col6);
		inThr.add(row13col6);
		inThr.add(row14col6);
		inThr.add(row15col6);
	}
	
	private void outThreeField(){
		
		outThr.add(row1col7);
		outThr.add(row2col7);
		outThr.add(row3col7);
		outThr.add(row4col7);
		outThr.add(row5col7);
		outThr.add(row6col7);
		outThr.add(row7col7);
		outThr.add(row8col7);
		outThr.add(row9col7);
		outThr.add(row10col7);
		outThr.add(row11col7);
		outThr.add(row12col7);
		outThr.add(row13col7);
		outThr.add(row14col7);
		outThr.add(row15col7);
	}
	
	
	private void totalFields(){

		totalFields.add(row1col8);
		totalFields.add(row2col8);
		totalFields.add(row3col8);
		totalFields.add(row4col8);
		totalFields.add(row5col8);
		totalFields.add(row6col8);
		totalFields.add(row7col8);
		totalFields.add(row8col8);
		totalFields.add(row9col8);
		totalFields.add(row10col8);
		totalFields.add(row11col8);
		totalFields.add(row12col8);
		totalFields.add(row13col8);
		totalFields.add(row14col8);
		totalFields.add(row15col8);
	}
	
	private void empIdFieds(){
		
		empId.add(row1col0);
		empId.add(row2col0);
		empId.add(row3col0);
		empId.add(row4col0);
		empId.add(row5col0);
		empId.add(row6col0);
		empId.add(row7col0);
		empId.add(row8col0);
		empId.add(row9col0);
		empId.add(row10col0);
		empId.add(row11col0);
		empId.add(row12col0);
		empId.add(row13col0);
		empId.add(row14col0);
		empId.add(row15col0);
		
	}
	private void empNameFieds(){
		
		empName.add(row1col1);
		empName.add(row2col1);
		empName.add(row3col1);
		empName.add(row4col1);
		empName.add(row5col1);
		empName.add(row6col1);
		empName.add(row7col1);
		empName.add(row8col1);
		empName.add(row9col1);
		empName.add(row10col1);
		empName.add(row11col1);
		empName.add(row12col1);
		empName.add(row13col1);
		empName.add(row14col1);
		empName.add(row15col1);
		
	}
	
	public void inOneFieldFocusHandler(final TextField field) {

		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						
						if (lostFocus) {
							try {
								
								String text = field.getText().trim();
								if(!text.equals("")){
									text = text.replace(".", ":");
									if(!PayRollUtility.validateTime(text)){
										
										showError("Enter valid in time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									
									CustomTime outField = outOne.get(inOne.indexOf(field));
									outField.setEditable(true);
									outField.setFocusTraversable(true);
									moveFocusToComp(outField);
								}else{
									
								}
								
							} catch (Exception exception) {
								
							}
						}
					}
				});
	}
	
	public void outOneFieldFocusHandler(final TextField field) {
		
		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						System.out.println("outOneFieldFocusHandler timeCountMap : "+timeCountMap);
						if (lostFocus) {
							try {
								String text = field.getText().trim();
								
								System.out.println("text = field  "+text);
								int index = outOne.indexOf(field);
								TextField total = totalFields.get(index);
								if(text.equals("")){
									
									CustomTime inField = inOne.get(index);
									inField.clear();
									field.setFocusTraversable(false);
									field.setEditable(false);
									
									ArrayList<String> list = timeCountMap.get(new Integer(index));
									
									if(list != null && list.size() > 0 && list.get(0) !=  null){
										String timeTOSub =  list.get(0);
										list.remove(0);
										 list.add(0,"00:00");
										 total.setText(PayRollUtility.subTractTime(timeTOSub, total.getText()));
									}
									
									inTwo.get(index).setEditable(false);
									inTwo.get(index).setFocusTraversable(false);
									
								}else{
									
									CustomTime inField = inOne.get(index);
									
									String inTime = inField.getText().trim();
									inTime = inTime.replace(".", ":");
									String outTime = field.getText().trim();
									outTime = outTime.replace(".", ":");
									
									if(!PayRollUtility.validateTime(outTime)){
										
										showError("Enter valid in out time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									String totalTime = "00:00";
									
									totalTime = PayRollUtility.subTractTime(inTime, outTime);
									
									if(timeCountMap.get(new Integer(index)) == null){
										timeCountMap.put(new Integer(index), new ArrayList<String>());
									}
									ArrayList<String> current = timeCountMap.get(index);
									System.out.println("current : "+current);
									
									if(current.size() > 0)
									current.remove(0);
									current.add(0, totalTime);
									
									if(current.size() > 0){
										
										total.setText(current.get(0));
									}
									if(current.size() > 1){
										total.setText(PayRollUtility.addTime(total.getText(), current.get(1)));
										
									}
									if(current.size() > 2){
										
										total.setText(PayRollUtility.addTime(total.getText(), current.get(2)));
									}
									System.out.println("current : "+current);
									
									inTwo.get(index).setEditable(true);
									inTwo.get(index).setFocusTraversable(true);
									moveFocusToComp(inTwo.get(index));
								
								}
								
								
							} catch (Exception exception) {
								exception.printStackTrace();
							}
						}
					}
				});
	}
	
	public void inTwoFieldFocusHandler(final TextField field) {

		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						
						if (lostFocus) {
							try {
								
								String text = field.getText().trim();
								if(!text.equals("")){
									text = text.replace(".", ":");
									if(!PayRollUtility.validateTime(text)){
										
										showError("Enter valid in time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									CustomTime outField = outTwo.get(inTwo.indexOf(field));
									outField.setEditable(true);
									outField.setFocusTraversable(true);
									moveFocusToComp(outField);
								}else{
									
								}
								
							} catch (Exception exception) {
								
							}
						}
					}
				});
	}
	
	public void outTwoFieldFocusHandler(final TextField field) {
		
		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						System.out.println("outTwoFieldFocusHandler timeCountMap : "+timeCountMap);
						if (lostFocus) {
							try {
								String text = field.getText().trim();
								
								System.out.println("text = field  "+text);
								int index = outTwo.indexOf(field);
								TextField total = totalFields.get(index);
								if(text.equals("")){
									
									CustomTime inField = inTwo.get(index);
									inField.clear();
									field.setFocusTraversable(false);
									field.setEditable(false);
									inField.setFocusTraversable(false);
									inField.setEditable(false);
									
									
									ArrayList<String> list = timeCountMap.get(new Integer(index));
									
									if(list != null && list.size() > 1 && list.get(1) !=  null){
										String timeTOSub =  list.get(1);
										list.remove(1);
										 list.add(1,"00:00");
										 total.setText(PayRollUtility.subTractTime(timeTOSub, total.getText()));
									}
									
									inThr.get(index).setEditable(false);
									inThr.get(index).setFocusTraversable(false);
								}else{
									
									CustomTime inField = inTwo.get(index);
									
									String inTime = inField.getText().trim();
									inTime = inTime.replace(".", ":");
									String outTime = field.getText().trim();
									outTime = outTime.replace(".", ":");
									
									if(!PayRollUtility.validateTime(outTime)){
										
										showError("Enter valid in out time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									String totalTime = "00:00";
									
									totalTime = PayRollUtility.subTractTime(inTime, outTime);
									
									if(timeCountMap.get(new Integer(index)) == null){
										timeCountMap.put(new Integer(index), new ArrayList<String>());
									}
									
									ArrayList<String> current = timeCountMap.get(index);
									if(current.size() > 1)
									current.remove(1);
									current.add(1, totalTime);
									inThr.get(index).setEditable(true);
									inThr.get(index).setFocusTraversable(true);
									moveFocusToComp(inThr.get(index));
									if(current.size() > 0){
										
										total.setText(current.get(0));
									}
									if(current.size() > 1){
										total.setText(PayRollUtility.addTime(total.getText(), current.get(1)));
										
									}
									if(current.size() > 2){
										
										total.setText(PayRollUtility.addTime(total.getText(), current.get(2)));
									}
								}
								
								
							} catch (Exception exception) {
								exception.printStackTrace();
							}
						}
					}
				});
	}
	
	public void inThrFieldFocusHandler(final TextField field) {

		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						
						if (lostFocus) {
							try {
								
								String text = field.getText().trim();
								if(!text.equals("")){
									text = text.replace(".", ":");
									if(!PayRollUtility.validateTime(text)){
										
										showError("Enter valid in time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									
									CustomTime outField = outThr.get(inThr.indexOf(field));
									outField.setEditable(true);
									outField.setFocusTraversable(true);
									moveFocusToComp(outField);
								}else{
									
								}
								
							} catch (Exception exception) {
								
							}
						}
					}
				});
	}
	
	public void outThrFieldFocusHandler(final TextField field) {
		
		field.focusedProperty().addListener(
				new ChangeListener<Boolean>() {
					@Override
					public void changed(ObservableValue<? extends Boolean> ov,
							Boolean lostFocus, Boolean getFocus) {
						System.out.println("outThrFieldFocusHandler timeCountMap : "+timeCountMap);
						if (lostFocus) {
							try {
								String text = field.getText().trim();
								
								System.out.println("text = field  "+text);
								int index = outThr.indexOf(field);
								TextField total = totalFields.get(index);
								if(text.equals("")){
									
									CustomTime inField =inThr.get(index);
									inField.clear();
									field.setFocusTraversable(false);
									field.setEditable(false);
									inField.setFocusTraversable(false);
									inField.setEditable(false);
									
									ArrayList<String> list = timeCountMap.get(new Integer(index));
									
									if(list != null && list.size() > 2 && list.get(2) !=  null){
										String timeTOSub =  list.get(2);
											list.remove(2);
											list.add(2,"00:00");
										 total.setText(PayRollUtility.subTractTime(timeTOSub, total.getText()));
									}
									
								}else{
									
									CustomTime inField = inThr.get(index);
									
									String inTime = inField.getText().trim();
									inTime = inTime.replace(".", ":");
									String outTime = field.getText().trim();
									outTime = outTime.replace(".", ":");
									
									if(!PayRollUtility.validateTime(outTime)){
										
										showError("Enter valid in out time");
										field.clear();
										moveFocusToComp(field);
										return;
										
									}
									String actual = PayRollUtility.subTractTime(inTime, outTime);
									String totalTime = "00:00";
									
									totalTime = PayRollUtility.subTractTime(inTime, outTime);
									
									
									if(timeCountMap.get(new Integer(index)) == null){
										timeCountMap.put(new Integer(index), new ArrayList<String>());
									}
									
									ArrayList<String> current = timeCountMap.get(index);
									if(current.size() > 2)
									current.remove(2);
									current.add(2, totalTime);
									
									if(current.size() > 0){
										
										total.setText(current.get(0));
									}
									if(current.size() > 1){
										total.setText(PayRollUtility.addTime(total.getText(), current.get(1)));
										
									}
									if(current.size() > 2){
										
										total.setText(PayRollUtility.addTime(total.getText(), current.get(2)));
									}
								}
								
								
							} catch (Exception exception) {
								
							}
						}
					}
				});
	}

	private void enable(CustomTime abc){
		abc.setEditable(true);
		abc.setFocusTraversable(true);
		moveFocusToComp(abc);
		moveFocusToComp(dataGrid);
	}
}
